﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace YJKGridTopologyCal.Geometries.Utilities
{
	public class LineStringExtracter : IGeometryFilter
	{
		public static ICollection<IGeometry> GetLines(IGeometry geom, ICollection<IGeometry> lines)
		{
			if (geom is ILineString)
			{
				lines.Add(geom);
			}
			else if (geom is IGeometryCollection)
			{
				geom.Apply(new LineStringExtracter(lines));
			}
			return lines;
		}

		public static ICollection<IGeometry> GetLines(IGeometry geom)
		{
			return LineStringExtracter.GetLines(geom, new List<IGeometry>());
		}

		public static IGeometry GetGeometry(IGeometry geom)
		{
			ICollection<IGeometry> lines = LineStringExtracter.GetLines(geom);
			return geom.Factory.BuildGeometry(lines);
		}

		public LineStringExtracter(ICollection<IGeometry> comps)
		{
			this._comps = comps;
		}

		public void Filter(IGeometry geom)
		{
			if (geom is ILineString)
			{
				this._comps.Add(geom);
			}
		}

		private readonly ICollection<IGeometry> _comps;
	}
}
